package com.kuqi.dao;

import com.kuqi.entity.ChapterInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface ChapterRepository extends JpaRepository<ChapterInfo, Integer>{

    // 根据书籍id查询所有章节(带章节内容)
    @Query(value = "select * from chapters where book_id =  ?1 order by chapter_index ",nativeQuery = true)
    List<ChapterInfo> findAllChaptersByBookId(int bookId);

    // 根据书籍id和index查询指定章节（带内容）
    @Query(value = "select * from chapters where book_id =  ?1 and chapter_index = ?2 limit 1",nativeQuery = true)
    ChapterInfo findChapterWithIndex(int bookId, int chapter_index);

    // 根据书籍id和index范围获取一部分章节 [minIndex, maxIndex)  （带内容）
    @Query(value = "select * from chapters where book_id =  ?1 and chapter_index between ?2 and ?3 order by chapter_index",nativeQuery = true)
    List<ChapterInfo> findChaptersWithSubsection(int bookId, int minIndex, int maxIndex);

}